<?php
use Workerman\Worker;
use Workerman\Lib\Timer;
use Workerman\Mysql\Connection;
require_once '../vendor/Workerman/Autoloader.php';
header("Content-type: text/html; charset=utf-8");
// 创建一个Worker监听2347端口，不使用任何应用层协议
$tcp_worker = new Worker("tcp://0.0.0.0:2347");
Worker::$logFile = '/var/www/workerman.log';
// 启动4个进程对外提供服务
$tcp_worker->count = 1;

// 当进程启动
$tcp_worker->onWorkerStart = function ($worker){
    $db = new Connection("47.96.30.0", "3306", "root", "root", "CP");
    $info= $db->query('SELECT code_time from config where id=1');
    $setinfo= $db->query('SELECT code_time from config where id=2');
    global $time,$setTime;
    $time = $info[0]['code_time'];
    $setTime = $setinfo[0]['code_time'];
    myTimer();
    setOpen();
};
// 运行worker
Worker::runAll();

/**
 * 请求时时彩接口
 */
function myTimer(){
    global $timer_id,$time;
    $timer_id = Timer::add($time, function(){
        global $timer_id,$time;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://cp.pf09.top/admin/Cp/ssc");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        $db = new Connection("47.96.30.0", "3306", "root", "root", "CP");
        $info= $db->query('SELECT code_time from config where id=1');
        $sscTime = $info[0]['code_time'];
        if ($time != $sscTime){
            Timer::del($timer_id);
            $time = $sscTime;
            myTimer();
        }
    });
}


function setOpen(){
    global $setTimer_id,$setTime;
    $setTimer_id = Timer::add($setTime, function(){
        global $setTimer_id,$setTime;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://cp.pf09.top/admin/Cp/ptc");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        $db = new Connection("47.96.30.0", "3306", "root", "root", "CP");
        $setinfo= $db->query('SELECT code_time from config where id=2');
        $setSscTime = $setinfo[0]['code_time'];
        if ($setTime != $setSscTime){
            Timer::del($setTimer_id);
            $setTime = $setSscTime;
            setOpen();
        }
    });
}